/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gps.decoder;

import pe.gps.record.Record;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import pe.gps.log.Log;

/**
 *
 * @author romulogalindo
 */
public class P1T extends Decoder {

    private int number_records;

    private final int HDOP = -74;
    private final int DIGOUT1 = -77;
    private final int DIGOUT2 = -76;
    private final int DIGIN1 = 1;
    private final int GPS_POWER = 69;
    private final int ODOMETER = -57;
    private final int SPEED = 24;
    private final int GSM_SIGNAL = 21;
    private final int CURRENT_BATTERY = 68;
    private final int BAT_VOLTAGE = 67;
    private final int EXTERNAL_VOLTAGE = 66;

    public P1T() {
        records = new ArrayList<>();
    }

    @Override
    public void decode(ByteBuffer buffer) {
    }

    private Character procesSenal_gps(int posicion) {
        if (posicion >= 40) {
            return 'E';
        } else if (posicion >= 30) {
            return 'D';
        } else if (posicion >= 20) {
            return 'C';
        } else if (posicion >= 10) {
            return 'B';
        } else {
            return 'A';
        }
    }

    private Character procesSenal_gsm(int posicion) {
        if (posicion == 1) {
            return 'E';
        } else if (posicion == 2) {
            return 'D';
        } else if (posicion == 3) {
            return 'C';
        } else if (posicion == 4) {
            return 'B';
        } else {
            return 'A';
        }

    }

    private Integer procesCharge(int posicion) {
        if (posicion >= 8000) {
            return 100;
        } else if (posicion >= 6000) {
            return 80;
        } else if (posicion >= 4000) {
            return 60;
        } else if (posicion >= 2000) {
            return 40;
        } else {
            return 20;
        }
    }

    @Override
    public ByteBuffer response() {
        switch (state) {
            case 1:
                ByteBuffer buffer = ByteBuffer.allocate(1);
                buffer.put((byte) 0x01);
                buffer.flip();
                return buffer;
            case 3:
                ByteBuffer buffer2 = ByteBuffer.allocate(4);
                buffer2.put(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) number_records});
                buffer2.flip();
                return buffer2;
            default:
                return null;
        }
    }

    @Override
    public String toString() {
        String string = "{ID=" + this.imei + "}";
        return string;
    }

    @Override
    public List<Record> decoderIMEI(ByteBuffer buffer) {
        int size = buffer.getShort(0);
        byte[] imei_byte = new byte[size];
        int cont = 0;
        for (int i = 2; i < size + 2; i++) {
            imei_byte[cont] = buffer.get(i);
            cont++;
        }
        String gr = imei;
        imei = new String(imei_byte);
        Log.log("[P1T][" + gr + " ->imei: " + imei + "->" + state + "][OK]");
        return new ArrayList<>();
    }

    //Devulve list si encuentra data o null si no puede
    @Override
    public List<Record> decoder(ByteBuffer buffer) {
        try {
            records = new ArrayList<>();
            if (state == 0) {
                //logueo
//                int size = buffer.getShort(0);
                String str = new String(buffer.array());
                imei = "P1T";
                System.out.println("Recibido:" + str);
                state = 1;
            } else if (state == 1 || state == 3) {
                String str = new String(buffer.array());
                System.out.println("Recibido:" + str);

                state = 3;
            }
        } catch (NumberFormatException ex) {
            Log.log("[P1T]Error desencriptando: " + ex, Log.ERROR);
            state = 3;
        }
        return records;
    }

}
